var shopCart = {};
//页面加载事件
$(function () {
    //初始化标签页
    shopCart.methods.tabInit(this);
    //初始化滚动
    shopCart.methods.SliderInit(this);
    //数字操作
    shopCart.methods.chooseNum(this);
    // 删除单个商品
    shopCart.methods.delete();
    // 删除选中商品
    shopCart.methods.deletes()
    // 计算合计
    shopCart.methods.total();
    // 店铺全选
    shopCart.methods.selectall();
    // 全局全选
    shopCart.methods.inverseSelection();
    // 用户全选
    shopCart.methods.userSelectAll();
    // 团队结算提示
    shopCart.methods.prompt();
});
shopCart.data = {};
shopCart.addEvent = function (obj) { };
shopCart.methods = {};
//初始化tab
shopCart.methods.tabInit = function (obj) {
    $(".shop-tabcontrol").each(function (val) {
        var tab = new Controller.TabControl({
            container: $(this),
            tabNavItem: ".shop-tab-nav-item",
            tabConItem: ".shop-tab-con-item",
            tagName: "data-id",
            event: "mouseover"
        });
    });
};
//初始化滚动
shopCart.methods.SliderInit = function (obj) {
    $(".shop-container").each(function (val) {
        Controller.Slider({
            father: $(this),
            container: ".shop-goods-list ul",
            prevBtn: ".shop-left-nav",
            nextBtn: ".shop-right-nav",
            itemSelector: "li",
            fatherSize: 1440
        });
    });
}
shopCart.methods.chooseNum = function (obj) {
    //数量减
    $(".shop-num-group .shop-num-substract").off("click").on("click", function () {
        if ($(this).hasClass("shop-disable")) {
            return;
        }
        var _val = $(this).siblings(".shop-num-input").val();
        var reg = /^[0-9]*$/;
        if (!reg.test(_val)) {
            _val = _val.replace(/[^0-9]/ig, "");
            $(this).siblings(".shop-num-input").val(_val);
            return;
        }
        var minNum = $(this).siblings(".shop-num-input").attr("data-min") | 0;
        var maxNum = $(this).siblings(".shop-num-input").attr("data-max") | 0;
        _val--;
        $(this).siblings(".shop-num-input").val(_val);
        shopCart.methods.numVal(_val, $(this).siblings(".shop-num-input"), minNum, maxNum);
        shopCart.methods.subtotal($(this)); //更新价格小计
        shopCart.methods.totalin(); //更新价格合计
    });
    //数量加
    $(".shop-num-group .shop-num-add").off("click").on("click", function () {
        if ($(this).hasClass("shop-disable")) {
            return;
        }
        var _val = $(this).siblings(".shop-num-input").val();
        var reg = /^[0-9]*$/;
        if (!reg.test(_val)) {
            _val = _val.replace(/[^0-9]/ig, "");
            $(this).siblings(".shop-num-input").val(_val);
            return;
        }
        var minNum = $(this).siblings(".shop-num-input").attr("data-min") | 0;
        var maxNum = $(this).siblings(".shop-num-input").attr("data-max") | 0;
        _val++;
        $(this).siblings(".shop-num-input").val(_val);
        shopCart.methods.numVal(_val, $(this).siblings(".shop-num-input"), minNum, maxNum);
        shopCart.methods.subtotal($(this)); //更新价格小计
        shopCart.methods.totalin(); //更新价格合计
    });
    //数量状态跟踪
    $(".shop-num-group .shop-num-input").off("input").on("input", function () {
        var _val = $(this).val();
        var reg = /^[0-9]*$/;
        if (!reg.test(_val)) {
            //截取，
            _val = _val.replace(/[^0-9]/ig, "");
            $(this).val(_val);
            return;
        }
        var minNum = $(this).attr("data-min") | 0;
        var maxNum = $(this).attr("data-max") | 0;
        shopCart.methods.numVal(_val, $(this), minNum, maxNum);
        shopCart.methods.subtotal($(this)); //更新价格小计
        shopCart.methods.totalin(); //更新价格合计
    });
}
//数量控制v
shopCart.methods.numVal = function (obj, cur, minNum, maxNum) {
    var _val = obj;
    if (obj) {
        //如果小于零，减禁用
        if (_val < minNum) {
            cur.val(minNum);
            cur.siblings(".substract").addClass("shop-disable");
        } else {
            cur.siblings(".substract").removeClass("shop-disable");
        }
        //如果，大于在售数，加禁用
        if (maxNum != 0 && _val > maxNum) {
            cur.siblings(".shop-num-add").addClass("shop-disable");
            cur.val(maxNum);
        } else {
            cur.siblings(".shop-num-add").removeClass("shop-disable");
        }
    } else {
        cur.val(minNum);
    }
}
// 更改商品选中状态
shopCart.methods.total = function () {
    $(".shop-goods .shop-cart-checkbox").off("change").on("change", function () {
        shopCart.methods.update();
    })
}



// 全局全选
shopCart.methods.inverseSelection = function () {
    $(".shop-cart-tootal .shop-cart-checkbox, .shop-collect .shop-cart-checkbox").off("change").on("change", function () {
        if ($(this).attr("checked")) {
            $(this).parents(".shop-cart").find(".shop-goods .shop-cart-checkbox").attr("checked", true);
        } else {
            $(this).parents(".shop-cart").find(".shop-goods .shop-cart-checkbox").attr("checked", false);
        }
        shopCart.methods.update();
    });
}
// // 用户全选
shopCart.methods.userSelectAll = function () {
    $(".shop-buyer>.shop-user-checkbox").off("change").on("change", function () {
        if ($(this).attr("checked")) {
            $(this).parents(".shop-users").find(".shop-goods .shop-cart-checkbox").attr("checked", true);
        } else {
            $(this).parents(".shop-users").find(".shop-goods .shop-cart-checkbox").attr("checked", false);
        }
        shopCart.methods.update();
    });
}
// 店铺全选
shopCart.methods.selectall = function () {
    $(".shop-store-title .shop-cart-checkbox").off("change").on("change", function () {
        if ($(this).attr("checked")) {
            $(this).parents(".shop-store").find(".shop-goods .shop-cart-checkbox").attr("checked", true);
        } else {
            $(this).parents(".shop-store").find(".shop-goods .shop-cart-checkbox").attr("checked", false);
        }
        shopCart.methods.update();
    });
}
// 整体更新
shopCart.methods.update = function () {
    shopCart.methods.totalin(); //更新合计
    shopCart.methods.selectedShop(); //更新店铺选中状态
    shopCart.methods.selectedUser(); //更新用户选中状态
    shopCart.methods.selectedAll(); //更新全选选中状态
    shopCart.methods.backgroundColor(); //更新背颜色
}
// 更新价格小计
shopCart.methods.subtotal = function (obj) {
    var quantity = parseInt(obj.parents(".shop-num-group").find(".shop-num-input").val()); //数量
    // console.log(obj.parent())
    var price = parseFloat(obj.parents(".shop-goods-item").find(".shop-ellips > .price").text().slice(1));
    var subtotal = (quantity * (price * 100)) / 100;
    obj.parents(".shop-goods-item").children(".col6").text("￥" + subtotal.toFixed(2));
}
// 更新合计价格
shopCart.methods.totalin = function () {
    var total = 0;
    var chosen = 0;
    $(".shop-goods .shop-cart-checkbox").each(function () {
        if ($(this).attr("checked")) {
            total += parseFloat($(this).parents(".shop-goods-item").children(".col6").text().slice(1))
            chosen++;
        }
    });
    $(".shop-font-money").html("&yen;" + total.toFixed(2));
    $(".shop-price").html(total.toFixed(2));
    $(".shop-font-num").html(chosen + "&nbsp;");
}
// 更新店铺选中状态
shopCart.methods.selectedShop = function () {
    $(".shop-store").each(function () {
        $(this).find(".shop-store-title .shop-cart-checkbox").attr("checked", this);
        $(this).find(".shop-goods .shop-cart-checkbox").each(function () {
            if (!$(this).attr("checked")) {
                $(this).parents(".shop-store").find(".shop-store-title .shop-cart-checkbox").attr("checked", false);
            }
        });
    });
}
// 更新用户选中状态
shopCart.methods.selectedUser = function () {
    $(".shop-buyer").each(function () {
        $(this).find(".shop-user-checkbox").attr("checked", this);
        $(this).parents(".shop-users").find(".shop-store .shop-cart-checkbox").each(function () {
            if (!$(this).attr("checked")) {
                $(this).parents(".shop-users").find(".shop-buyer .shop-user-checkbox").attr("checked", false);
            }
        });
    });
}
// 更新全选选中状态
shopCart.methods.selectedAll = function () {
    $(".shop-cart-tootal .shop-cart-checkbox, .shop-collect .shop-cart-checkbox").attr("checked", true);
    $(".shop-goods .shop-cart-checkbox").each(function () {
        if (!$(this).attr("checked")) {
            $(".shop-cart-tootal .shop-cart-checkbox, .shop-collect .shop-cart-checkbox").attr("checked", false);
        }
    });
}
// 更新背景色
shopCart.methods.backgroundColor = function () {
    $(".shop-goods").each(function () {
        if ($(this).find(".shop-cart-checkbox").attr("checked")) {
            $(this).addClass("ProductSelection");
        } else {
            $(this).removeClass("ProductSelection");
        }
    });
    $(".shop-store-title").each(function () {
        if ($(this).find(".shop-cart-checkbox").attr("checked")) {
            $(this).siblings(".shop-store-shipping").addClass("ProductSelection");
            $(this).addClass("ProductSelection");
        } else {
            $(this).removeClass("ProductSelection");
            $(this).siblings(".shop-store-shipping").removeClass("ProductSelection");
            $(this).removeClass("ProductSelection");
        }
    });
}
// 隐藏团体结算提示
shopCart.methods.prompt = function () {
    $(".shop-cart .shop-collect > .shop-prompt > img").on("click", function () {
        $(".shop-cart .shop-collect > .shop-prompt").css("display", "none");
    });
}
// 删除单个商品
shopCart.methods.delete = function () {
    $(".shop-btn-group .delete").off("click").on("click", function () {
        if ($(this).parents(".shop-store").find(".shop-goods").length == 1) {
            $(this).parents(".shop-store").remove();
        } else {
            $(this).parents(".shop-goods").remove();
        }
        shopCart.methods.update();
    })
}
// 删除选中商品
shopCart.methods.deletes = function () {
    $(".shop-collect .shop-btn-delete").off("click").on("click", function () {
        $(".shop-goods .shop-cart-checkbox").each(function () {
            if($(this).attr("checked")){
                if ($(this).parents(".shop-store").find(".shop-goods").length == 1) {
                    $(this).parents(".shop-store").remove();
                } else {
                    $(this).parents(".shop-goods").remove();
                }
            }
        })
        shopCart.methods.update();
    })
}